﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OptimumInfra.Models;
using System.Text.RegularExpressions;

namespace OptimumInfra.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        private EmailRepository repo = new EmailRepository();
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to Optimum Infra!";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }
        public ActionResult Whyus()
        {
            return View();
        }
        public ActionResult Contactus()
        {
            return View();
        }
        public string UpdateForm(string email)
        {
            if (email != "Enter text" && email != null && !email.Equals(""))
            {
                string pattern = @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";
                Match match = Regex.Match(email.Trim(), pattern, RegexOptions.IgnoreCase);
                if (!match.Success)
                     return "Please enter valid email";

                Newsletter news = new Newsletter();
                news.Email = email;
                this.repo.CreateNewEmail(news);
                return "Your email was succesfull subscribed";
            }

            return String.Empty;
        }

    }
}
